//
//  MainController.m
//  blog_oc
//
//  Created by Tanenbaum on 15/7/28.
//  Copyright © 2015年 GZJ. All rights reserved.
//

#import "MainController.h"
#import "HomeController.h"
#import "MessageController.h"
#import "DiscoverController.h"
#import "ProfileController.h"

@interface MainController ()

@property(nonatomic,strong) UIButton  *button;

@end

@implementation MainController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addChildViewControllers];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    [self setUpbutton];
}
//统一给各个视图控制器设置相关属性赋值
- (void)addChildViewControllers {
    
    
    [self addChildViewController:[[HomeController alloc]init] andTitle:@"首页" andImage:@"tabbar_home"];
    
    [self addChildViewController:[[MessageController alloc]init] andTitle:@"消息" andImage:@"tabbar_message_center"];
    [self addChildViewController:[[UIViewController alloc]init]];
    
    [self addChildViewController:[[DiscoverController alloc]init] andTitle:@"发现" andImage:@"tabbar_discover"];
    [self addChildViewController:[[ProfileController alloc]init] andTitle:@"我" andImage:@"tabbar_profile"];
    
}


//设置各个控制器的标题和背景图片，并植入导航控制器
- (void)addChildViewController:(nonnull UIViewController *)childController andTitle:(NSString *)title andImage:(NSString *)imageNamed {
    
    self.tabBar.tintColor = [UIColor orangeColor];
    
    childController.tabBarItem.image = [UIImage imageNamed:imageNamed];
    childController.title = title;
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:childController];
    
    [self addChildViewController:nav];
    
    
}

- (void)ClickButton  {
    
    NSLog(@"%s",__FUNCTION__);
}

//设置图标的位置属性

- (void)setUpbutton {
    
    CGFloat w = self.tabBar.bounds.size.width / self.viewControllers.count ;
    
    CGRect rect = CGRectMake(0, 0, w, self.tabBar.bounds.size.height);
    
    self.button.frame = CGRectOffset(rect, 2 * w, 0);
    
    [self.button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [self.button  setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    
    [self.button  setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [self.button  setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    [self.button addTarget:self action:@selector(ClickButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tabBar addSubview:self.button ];
    
}

//懒加载

- (UIButton *)button {
    
    if (_button == nil) {
        
        _button = [[UIButton alloc]init];
    }
    
  
    return _button;
}

@end
